package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.Bindable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;

import com.yzp.video.view.pdf.util.Util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import chuangyi.com.org.DOMIHome.BR;

import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.PopupActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.SaveContent;
import chuangyi.com.org.DOMIHome.Utils.OSSUtil;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.WaitActivityResult;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/20.
 */

public class UserInfoModel extends ObservableWithGoBackHandler implements SaveContent, WaitActivityResult {
    private String avatar;
    private String nickname;
    private String company;
    private String phone;
    private boolean expertflag;
    private boolean appling;
    private int wdgz;
    private int wdfs;
    private int wdxx;
    private String job;
    private String realname;
    private String zone;
    private String industry;
    private boolean loading;
    private ListBindingAdapter<MenuItem> jobList;
    private ListBindingAdapter<MenuItem> industryList;

    @Bindable
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
        notifyPropertyChanged(BR.avatar);
    }

    @Bindable
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
        notifyPropertyChanged(BR.nickname);
    }

    @Bindable
    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
        notifyPropertyChanged(BR.company);
    }

    @Bindable
    public boolean isExpertflag() {
        return expertflag;
    }

    public void setExpertflag(boolean expertflag) {
        this.expertflag = expertflag;
        notifyPropertyChanged(BR.expertflag);
    }

    @Bindable
    public int getWdgz() {
        return wdgz;
    }

    public void setWdgz(int wdgz) {
        this.wdgz = wdgz;
        notifyPropertyChanged(BR.wdgz);
    }

    @Bindable
    public int getWdfs() {
        return wdfs;
    }

    public void setWdfs(int wdfs) {
        this.wdfs = wdfs;
        notifyPropertyChanged(BR.wdfs);
    }

    @Bindable
    public int getWdxx() {
        return wdxx;
    }

    public void setWdxx(int wdxx) {
        this.wdxx = wdxx;
        notifyPropertyChanged(BR.wdxx);
    }

    @Bindable
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
        notifyPropertyChanged(BR.job);
    }

    @Bindable
    public String getPhone() {
//        if (phone == null || "".equals(phone))
//            return "未绑定";
        return User.getCurrentUser().getPhone();
    }

//    public void setPhone(String phone) {
//        this.phone = phone;
//        notifyPropertyChanged(BR.phone);
//    }

    @Bindable
    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
        notifyPropertyChanged(BR.realname);
    }

    @Bindable
    public String getZone() {
        return zone;
    }

    public void setZone(String zone) {
        this.zone = zone;
        notifyPropertyChanged(BR.zone);
    }

    @Bindable
    public String getIndustry() {
        return industry;
    }

    public void setIndustry(String industry) {
        this.industry = industry;
        notifyPropertyChanged(BR.industry);
    }

    public ListBindingAdapter<MenuItem> getJobList() {
        return jobList;
    }

    public ListBindingAdapter<MenuItem> getIndustryList() {
        return industryList;
    }

    @Bindable
    public boolean isAppling() {
        return appling;
    }

    public void setAppling(boolean appling) {
        this.appling = appling;
    }

    public UserInfoModel(Context context) {
        super(context);
        super.getHandler().setTitle("完善个人资料");
        this.phone = User.getCurrentUser().getPhone();
        jobList = new ListBindingAdapter<MenuItem>(getContext(), BR.menu);
        industryList = new ListBindingAdapter<MenuItem>(getContext(), BR.menu);
    }

    public UserInfoModel clone() {
        UserInfoModel model = new UserInfoModel(getContext());
        model.avatar = this.avatar;
        model.company = this.company;
        model.job = this.job;
        model.industry = this.industry;
        model.expertflag = this.expertflag;
        model.nickname = this.nickname;
        model.wdfs = this.wdfs;
        model.wdgz = this.wdgz;
        model.wdxx = this.wdxx;
        model.realname = this.realname;
        model.phone = this.phone;
        model.zone = this.zone;
        return model;
    }

    public void getMoreInfo() {
        if (loading)
            return;
        loading = true;
        jobList.clear();
//        industryList.clear();
        notifyDataSetChanged(jobList);
//        notifyDataSetChanged(industryList);
        WebApi.getMoreUserInfo(getContext(), this);
        List<String> list = WebApi.getJobList(getContext());
        if (list == null) {
            list = new ArrayList<String>();
        }

        for (String job : list) {
            MenuItem item = new MenuItem(getContext(), R.layout.i_menu);
            item.setTitle(job);
            item.setCode(job);
            jobList.add(item);
        }

        notifyDataSetChanged(jobList);
    }

    public void onClickPhoto(View view) {
        Activity activity = (Activity) getContext();
        Intent intent = new Intent();
        intent.setClass(activity, PopupActivity.class);
        PopupActivity.setDataModel(new SelectPhotoModel(activity));
        intent.putExtra("layoutId", R.layout.v_dialog_modify_photo);
        intent.putExtra("variableId", BR.photoSelector);
        activity.startActivityForResult(intent, HomeActivity.REQUEST_IMAGE);
    }

    /**
     * 申请成为专家，进入下一步
     *
     * @param view
     */
    public void onClickNextStep(View view) {
        if (!validateInput()) {
            return;
        }
        final UserInfoModel self = this;
        LoadingDialog.getInstance(getContext()).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!WebApi.saveUserInfo(getContext(), self)) {
                    return;
                }
                int result = WebApi.isDone(getContext());
                if (result == CommunicationModel.GOTO_PROGRESS_PAGE) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ApplyExpertContractModel model = new ApplyExpertContractModel(getContext());
                            CommonFragment fragment = new CommonFragment();
                            fragment.setLayoutId(R.layout.f_apply_expert_contract);
                            fragment.setVariableId(BR.contract);
                            fragment.setDataModel(model);
                            HomeActivity.cancelFragment();
                            HomeActivity.switchFragment((Activity) getContext(), fragment);
                        }
                    });
                } else if (result == CommunicationModel.GOTO_COMPLETE_INFO) {
                    showToast("请完善个人资料");
                }
                LoadingDialog.getInstance(getContext()).dismiss();
            }
        }).start();
    }

    private boolean validateInput() {
        if (Utils.isNullOrEmpty(getAvatar()) || Utils.isNullOrEmpty(getCompany())
                || Utils.isNullOrEmpty(getIndustry()) || Utils.isNullOrEmpty(getJob())
                || Utils.isNullOrEmpty(getNickname()) || Utils.isNullOrEmpty(getRealname())
                || Utils.isNullOrEmpty(getZone())) {
            showToast("请填写所有必填项");
            return false;
        }
        return true;
    }

    @Override
    public boolean check() {
        return true;
    }

    @Override
    public void save() {
        HomeActivity.cancelFragment();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case HomeActivity.REQUEST_IMAGE:
                    LoadingDialog.getInstance(getContext()).show();
                    Uri uri = data.getData();
                    final String fileName = Utils.getPathByUri(uri, getContext());
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String url = OSSUtil.uploadFile(getContext().getApplicationContext(), fileName);
                            setAvatar(url);
                            LoadingDialog.getInstance(getContext()).dismiss();
                        }
                    }).start();
                    break;
                case HomeActivity.REQUEST_INDUSTRY:
                    if (data != null) {
                        setIndustry(data.getStringExtra("code"));
                    }
                    break;
                case HomeActivity.REQUEST_JOB:
                    if (data != null) {
                        setJob(data.getStringExtra("code"));
                    }
                    break;
            }
        }
    }

    public void onSelectIndustry(View view) {
        Activity activity = (Activity) getContext();
        OptionSelector selector = new OptionSelector(getContext(), "行业");
        selector.setOptions(industryList);
        PopupActivity.startActivityForResult(activity, R.layout.v_dialog_option, HomeActivity.REQUEST_INDUSTRY,
                BR.selector, selector);
    }

    public void onSelectJob(View view) {
        Activity activity = (Activity) getContext();
        OptionSelector selector = new OptionSelector(getContext(), "职位");
        selector.setOptions(jobList);
        PopupActivity.startActivityForResult(activity, R.layout.v_dialog_option, HomeActivity.REQUEST_JOB,
                BR.selector, selector);
    }

    public void onClickSave(View view) {
        if (!validateInput()) {
            return;
        }
        final UserInfoModel self = this;
        LoadingDialog.getInstance(getContext()).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (WebApi.saveUserInfo(getContext(), self)) {
                    showToast("保存成功");
                }
                LoadingDialog.getInstance(getContext()).dismiss();
            }
        }).start();
    }
}
